Having trouble getting the words to show up on an image mask for a word cloud in R.
Using this Simpsons PNG (https://imgbin.com/png/PV5MuKbG/lisa-simpson-bart-simpson-homer-simpson-maggie-simpson-mayor-quimby-png)
Code is below:
wc1 <- sort(table(bplot_one$word), decreasing = TRUE)
figPath <- "Simpsons.png"
wordcloud2(wc1, figPath = figPath)
It executed fine, but all I get is the png without the words
Any idea how to fix this?
Thanks
This is listed as an open issue on the package site: Fig Mask and lettercloud are not working with package installed from github #68. There is a workaround posted: mask and letterCloud silently fail #12.
The workaround is to refresh the viewer or open in a browser.
Related
I am working on a word cloud shaped by an image on a jpg or png file. I am using R and its wordcloud2 package with figPath option.
Issuing the following command:
wordcloud2(Word_more_freq,figPath = "unicorno.jpg",size = 1.5, color = "skyblue"),
where Word_more_freq is a data frame and its columns are words and freq (words' frequency), sometimes no image is displayed and other times I can just view the image without the words. No error message is raised.
Could you help me please?
Thanks in advance.
Cheers,
Francesco
I am trying to mine text from a bunch of PDFs, but when I read them into R using pdf_text from the pdftools package, the text it produces is just strange and nothing like what is actually on the PDF file. Onedrive link: https://1drv.ms/b/s!AlTtlgN0WIa3s2qeq4yrv9fUu-Z6 .
Here's the sample code I use:
library(pdftools)
pdf1 <- pdf_text("https://dl.dropboxusercontent.com/s/308gpdijvnw18mf/2018REQ118030709.pdf?dl=0")
pdf1
## c("(’-*)&&$(&’-’’’’)*,&’$)’&/.\r\n itiCHMON&\\ 4Q\\a WN BQKPUWVL
##FQZOQVQI )’(/ 7QZ[\\ 9ITN BMIT
##6[\\I\\M DI‘ 3QTT\r\n 5Q^Q[QWV WN 4WTTMK\\QWV[\r\n
##FE 8_h -10+0\r\n HYSX]_^T’ L7 -.-1,(10+0
##3QTT >]UJMZ (/’*’.’0\r\n IBKHHO F7L;HI ?D9
###TMI[M ZMKWZL 3QTT >]UJMZ QV UMUW [MK\\QWV WN KPMKS\r\n ,0+, L7BB;O H:\r\n
##H?9>CED: L7 -.---(0/+1
##IVL QVKT]LM QV ITT WVTQVM JIVSQVO \\ZIV[IK\\QWV[\r\n
###ZWXMZ\\a :VNWZUI\\QWV
##DI‘ :VNWZUI\\QWV\r\n JQh OUQb5
##-+,3 J_dQ\\ 7TZecdUT 7^^eQ\\ 9XQbWUc5
##!,+’/+/)++\r\n 3QTT >]UJMZ1 .
##.. <truncated>
I am pretty new to R, any idea what I may be doing wrong?
Please, any help with this would be appreciated.
Edit: I have replaced the url with a working url and I have also included the results that I am getting.
You pdf is a pdf image. It looks like a scan. pdftools cannot convert this directly into text. You can use the package tesseract to get the data and pdftools to convert it into an png.
Code below will transform the first page into text. I will let you do the rest of the pages. Rembember that OCR to text isn't perfect. You need to check the outcome.
library(pdftools)
library(tesseract)
pdf_convert("https://dl.dropboxusercontent.com/s/308gpdijvnw18mf/2018REQ118030709.pdf?dl=0",
pages = 1,
dpi = 600,
filenames = "page1.png")
text <- ocr("page1.png")
cat(text)
More information is available in the tesseract vignette.
You also might want to remove access to this pdf. I'm not sure it this data should be publicly available
Summary: my ultimate goal is to use rCharts, and specifically Highcharts, as part of a ReporteRs PowerPoint report automation workflow. One of the charts I would like to use is rendered as html in the Viewer pane in Rstudio, and addPlot(function() print(myChart)) does not add it to the PowerPoint. As a workaround, I decided to try to save myChart to disk, from where I could just add it to the PowerPoint that way.
So my question is really, How do I get my html image into my ReporteRs workflow? Either getting it saved to a disk, or getting it to be readable by ReporteRs would solve my problem.
This question is really the same as this one, but I'm using rCharts, specifically the example found here:
#if the packages are not already installed
install.packages('devtools')
require(devtools)
install_github('rCharts', 'ramnathv')
#code creates a radar chart using Highcharts
library(rCharts)
#create dummy dataframe with number ranging from 0 to 1
df<-data.frame(id=c("a","b","c","d","e"),val1=runif(5,0,1),val2=runif(5,0,1))
#muliply number by 100 to get percentage
df[,-1]<-df[,-1]*100
myChart <- Highcharts$new()
myChart$chart(polar = TRUE, type = "line",height=500)
myChart$xAxis(categories=df$id, tickmarkPlacement= 'on', lineWidth= 0)
myChart$yAxis(gridLineInterpolation= 'circle', lineWidth= 0, min= 0,max=100,endOnTick=T,tickInterval=10)
myChart$series(data = df[,"val1"],name = "Series 1", pointPlacement="on")
myChart$series(data = df[,"val2"],name = "Series 2", pointPlacement="on")
myChart
So if I try
> png(filename="~/Documents/name.png")
> plot(myChart)
Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'
> dev.off()
I get that error.
I've looked into Highcharts documentation, as well as many other potential solutions that seem to rely on Javascript and phantomjs. If your answer relies on phantomjs, please assume I have no idea how to use it. webshot is another package I found which is even so kind as to include an install_phantomjs() function, but from what I could find, it requires you to turn your output into a Shiny object first.
My question is really a duplicate of this one, which is not a duplicate of this one because that is how to embed the html output in Rmarkdown, not save it as a file on the hard drive.
I also found this unanswered question which is also basically the same.
edit: as noted by #hrbrmstr and scores of others, radar charts are not always the best visualization tools. I find myself required to make one for this report.
The answer turned out to be in the webshot package. #hrbrmstr provided the following code, which would be run at the end of the code I posted in the question:
# If necessary
install.packages("webshot")
library(webshot)
install_phantomjs()
# Main code
myChart$save("/tmp/rcharts.html")
webshot::webshot("/tmp/rcharts.html", file="/tmp/out.png", delay=2)
This saves the plot to the folder as an html, and then takes a picture of it, which is saved as a png.
I can then run the ReporteRs workflow by using addImage(mydoc, "/tmp/out.png").
I'm looking for a way to render my plot_ly plot directly to a browser in stead of r-studios default viewer. I've searched the plotly documentation but I only see a reference to the default behavior of opening the plot to a browser when running r from a terminal.
Does anyone know how to open to a browser window by default? Maybe a parameter to the plotly layout() option?
Alright I found a simple solution in the related questions section next to my original question. Sorry I didn't find it before. stackoverflow.com/questions/36868743 .
Setting: options(viewer=NULL) in the script disables the viewer and opens my plot in the browser.
Not really elegant and how to turn the default viewer back on is still a little mystery.
An example from this site might help:
http://www.statsblogs.com/2014/02/06/protected-online-r-and-plotly-graphs-canadian-and-u-s-maps-old-faithful-with-multiple-axes-overlaid-histograms/
library(plotly)
p <- plotly(username="MattSundquist", key="4om2jxmhmn")
library(maps)
data(canada.cities)
trace1 <- list(x=map(regions="canada")$x,
y=map(regions="canada")$y)
trace2 <- list(x= canada.cities$long,
y=canada.cities$lat,
text=canada.cities$name,
type="scatter",
mode="markers",
marker=list(
"size"=sqrt(canada.cities$pop/max(canada.cities$pop))*100,
"opacity"=0.5)
)
response <- p$plotly(trace1,trace2)
url <- response$url
filename <- response$filename
browseURL(response$url)
The main take home being browseURL(response$url). Notice the sign in as well.
My goal is to download an image from an URL and then display it in R.
I got an URL and figured out how to download it. But the downloaded file can't be previewed because it is 'damaged, corrupted, or is too big'.
y = "http://upload.wikimedia.org/wikipedia/commons/5/5d/AaronEckhart10TIFF.jpg"
download.file(y, 'y.jpg')
I also tried
image('y.jpg')
in R, but the error message shows like:
Error in image.default("y.jpg") : argument must be matrix-like
Any suggestions?
If I try your code it looks like the image is downloaded. However, when opened with windows image viewer it also says it is corrupt.
The reason for this is that you don't have specified the mode in the download.file statement.
Try this:
download.file(y,'y.jpg', mode = 'wb')
For more info about the mode is see ?download.file
This way at least the file that you downloaded is working.
To view the image in R, have a look at
jj <- readJPEG("y.jpg",native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)
or how to read.jpeg in R 2.15
or Displaying images in R in version 3.1.0
this could work too
here
library("jpeg")
library("png")
x <- "http://upload.wikimedia.org/wikipedia/commons/5/5d/AaronEckhart10TIFF.jpg"
image_name<- readJPEG(getURLContent(x)) # for jpg
image_name<- readPNG(getURLContent(x)) # for png
After downloading the image, you can use base R to open the file using your default image viewer program like this:
file.show(yourfilename)