Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:02:27+00:00 2026-06-05T20:02:27+00:00

Here is my function: # Purpose: Scrape Floraweb.de for plant species data (photograph, sociology,

  • 0

Here is my function:

# Purpose: Scrape Floraweb.de for plant species data (photograph, sociology, ecology, anatomy)
# Author: Kay Cichini
# Date: 2012-06-10
# Output: PDF saved to created folder .~/FLORAWEB
# Packages: XML, jpeg, 
# Licence: cc by-nc-sa

floraweb_scraper <- function(input) {

    # I didn't get around this encoding issue other than with gsub..
    spch_sub <- function(x) {
        x <- gsub("ü", "ü", x)
        x <- gsub("ä", "ä", x)
        x <- gsub("ö", "ö", x)
        x <- gsub("Ä", "Ä", x)
        x <- gsub("Ãoe", "Ü", x)
        x <- gsub("ü", "Ä", x)
        x <- gsub("Ö", "Ö", x)
        x <- gsub("ß", "ß", x)
        x <- gsub("é", "é", x)
        x <- gsub("Ã-", "í", x)
        x <- gsub("á", "á", x)
        x <- gsub("±", "~", x)
        x <- gsub(" ", "", x)  # pattern for backspaces
    }

    # automated package installation:
    pkgs <- c("XML", "jpeg")

    pkgs_miss <- pkgs[which(!pkgs %in% installed.packages()[, 1])]
    if (length(pkgs_miss) > 0) {
        install.packages(pkgs_miss)
    }

    # load packages:
    require(XML)
    require(jpeg)


    # prepare input and get parsed script:
    input1 <- gsub("[[:space:]]", "+", input)
    URL <- paste("http://www.floraweb.de/pflanzenarten/taxoquery.xsql?taxname=", 
                 input1, sep = "")
    doc <- htmlParse(URL)

    # get returned species names (dismiss last row with additional info):
    sp <- xpathSApply(doc, "//div[@id='contentblock']//a", xmlValue)
    sp <- sp[1:length(sp)-1]

    # get species ids from contentblock:
    con <- getNodeSet(doc, "//div[@id='contentblock']//a")[1:len]
    urls <- sapply(con, xmlGetAttr, "href")
    id_1 <- gsub("[^0-9]", "", urls)

    # check matching and assign to resulting dataframe:
    match <- numeric()
    for (i in 1:len) {
        match[i] <- sum(unlist(strsplit(tolower(sp), " ")[i]) %in% unlist(strsplit(input, 
            " ")) == 0)
    }
    df <- data.frame(sp, id_1, match)

    # select the one with best match:
    sel <- id_1[rank(df$match)][1]

    # build urls for retrieving species data
    url <- paste("http://www.floraweb.de/pflanzenarten/druck.xsql?suchnr=", sel, sep = "")

    doc <- htmlParse(url)
    img_src <- xpathSApply(doc, '//*/p[@class="centeredcontent"]/img/@src')
    img_url <- gsub("../", "http://www.floraweb.de/", img_src, fixed = T)

    # get infos:
    infos <- xpathSApply(doc, "//div[@id='content']//p", xmlValue)[c(2, 7, 22, 33, 35, 14)]

    # replace special characters:
    infos <- spch_sub(infos)

    # make dir to save data:
    dir.create(path.expand("~/FLORAWEB/"), showWarnings = F)
    setwd(path.expand("~/FLORAWEB/"))

    # download image:
    download.file(img_url, "floraweb.jpg", mode = "wb")

    # open device:
    pdf(paste(spch_sub(df$sp[df$id_1 == sel]), ".FloraWeb.pdf", sep = ""), paper = "a4")

    # read image:
    img <- readJPEG("floraweb.jpg")
    w <- dim(img)[2]
    h <- dim(img)[1]

    # print img to plot region:
    par(mar = rep(0, 4), oma = rep(0, 4), mfrow = c(2, 1))
    plot(NA, xlim = c(0, w), ylim = c(0, h), xlab = "", ylab = "", axes = F, 
    type = "n", yaxs = "i", xaxs = "i", asp = 1)
    rasterImage(img, 0, 0, w, h)

    # print text:
    plot(NA, xlim = c(0, 1), ylim = c(0, 1), xlab = "", ylab = "", axes = F, type = "n", 
        yaxs = "i", xaxs = "i")
    # text left intendent and center adjustment:
    l <- 0.5
    c_adj <- c(0.5, 0.5)

    # plot text:
    text(l, 0.95, paste("Eingabe = ", input, " / Gefunden = ", infos[1], sep = ""), font = 2, adj = c_adj, cex = 0.7)
    text(l, 0.5, paste(strwrap(infos[-1], width = 112), collapse = "\n"), adj = c_adj, cex = 0.7)

    # Credit:
    text(l, 0.05, "Die hier verwendeten Daten sind der Internet-Seite FloraWeb.de entnommen.", 
        adj = c_adj, cex = 0.4, font = 3)

    graphics.off()
    message(paste(sp_name, "PDF wurde erzeugt\n\n", sep = "\n -- "))

    # remove jpegs:
    unlink(dir(pattern = ".jpg"))
}

# Examples:
floraweb_scraper("Poa alp")

pfl_liste <- c("leuc alp", "Poa badensis", "Poa alp")
lapply(pfl_liste, FUN = floraweb_scraper)

It works well in the first example but throws error when used with lapply –
does anyone have a clue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:02:30+00:00Added an answer on June 5, 2026 at 8:02 pm

    object len is undefined

    for (i in 1:len) {
            match[i] <- sum(unlist(strsplit(tolower(sp), " ")[i]) %in% unlist(strsplit(input, 
                " ")) == 0)
        }
    

    you probably have it defined in your workspace. When you call the first function everything is ok. Maybe you have problems with scope when using lapply. Defining len in your function may resolve matters

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title is pretty self explanatory. Here's the function I've written for this purpose:
Here is my function : //Data Structures : abstract class HuffmanTree case class Node(left:
I have two functions here function Preloader() {} Preloader.prototype = { init:function() { //
I have a simple Jquery function here: function submit_selection(del,submit) { del='yes'; submit=true; var ITAMZ=new
Here is function , <script type=text/javascript> $(document).ready(function() { getRecordspage(1, 5); $(a.page-numbers).click(function() { alert(1); getRecordspage($(this).text(),
$('#selector').click(function() { // here I would like to load a javascript file // let's
I've got a function here that is meant to look through a list of
simonn helped me to code an ordered integer partition function here. He posted two
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I need to pass an url from asp.net load_page to flowplayer javascript function here:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.