Having read the docs I think this is unlikely, but decide to ask regardless.
I'm writing a poster, and the tabular has to stay within the center environment instead of table environment. (The table environment is a float, which does not work within the boxes of a poster).
This leads to the need for \captionof instead of \caption to put a caption inside a center environment. Is xtable capable of such a thing?
\caption is hard coded. See source of print.xtable.R.
if (tabular.environment == "longtable" && caption.placement == "top") {
if (is.null(short.caption)){
BCAPTION <- "\\caption{"
} else {
BCAPTION <- paste("\\caption[", short.caption, "]{", sep = "")
}
Way around this would be to do gsub on the result before you pass it to the interpreter. Something along the lines of gsub("\\caption", "\captionof", x).
Related
The issue is with the Hiding example (subset example with checkboxes):
helloworld/main/examples/web-ifc-viewer/hiding/bundle.js
When a subset is not-displayed, hover still highlights the element in the non-displayed subset. The solution as proposed on Discord was to remove the subset model from the array: ifcviewer.context.items.pickableIfcModels,
but it's not clear to me how or where to do this. Thanks for any help.
most probably because " ifcviewer.context.items.pickableIfcModels" is still thinks that you're dealing with the model as a whole not with the subsets,
you need to add this function:
function togglePickable(mesh, isPickable)
pickable = viewer.context.items.pickableIfcModels
if(isPickable) {
pickable.push(mesh)
} else {
const index = pickable.indexof(mesh)
pickable.splice(index, 1)
}
and on setupCheckbox function add this line under
if(checkbox.checked){
..
togglePickable(subset, true)
}else{
...
togglePickable(subset, false)
}
and on setupCategory function add the following:
togglePickable(subset[category], true)
and on your load model function you also need to add:
togglePickable("your model", true)
and it shall work ^^
I would like to see bigger plotly graphs while doing EDA in R Markdown. If I change the graph size through plotly functions like ggplotly or plot_ly, RStudio creates scroll bars and wouldn't show more than 800px width.
Here is my code:
gg1 <- cars |>
ggplot(aes(speed, dist)) +
geom_point()
gg1 |>
plotly::ggplotly(width = 1000, height = 800)
This is what I see:
Things I tried but didn't do anything...
out.width
fig.width
RStudio Tools > Global Options > R Markdown > Visual > Editor content width (I don't know if this does anything)
Now, if I output to console instead of inline or knit to html, then the code prints out bigger graphic without any error. But I would like to see bigger graphics while in RStudio R Markdown interactive session inline.
I couldn't find a way that works as a setting in R or R Markdown. However, I can tell you how to set this temporarily.
You will use:
RStudio's developer tools (standard part of RStudio)
two sets of function calls written in Javascript
This is temporary. To reset the chunk outputs back to normal:
collapse the chunk
rerun the chunk
restart RStudio
If you add chunks after you run the functions, they will not inherit these changes. If want them to change, rerun the functions.
The first line of each set of functions is variable initialization. Skip that first line of code if you run the functions a second time (or more).
Here's what to do:
Anywhere in RStudio, right-click, select "Inspect Element"
This will open developer tools. In the developer tools window, open the console drawer. You can select the three dots on the top right and use the menu to do this.
In the console drawer, you will paste the functions I've provided. However, the calls for the variables can only be done one time. So the first time you run this in the same session of RStudio you will include that line, after that, do not. When you restart RStudio, this resets. You will need to include that line again. (If you restart developer tools, this will not reset these variables.)
The console is at the bottom of developer tools.
Here is the before...
Here's an example of a copy and paste, first time in the RStudio session.
You will immediately see that the width has changed for any chunks with output.
However, you may notice that the overflow is hidden. That's next.
You will enter the second function into the console drawer to fix change that.
It won't look different this time!
When you look at the chunk output, it will still look like content is hidden.
From here you have to drag the panes, to force resizing. Make it bigger, smaller, or even return to the size you had to begin with. The overflow will be shown.
Last, but certainly not least--the Javascript functions.
The first:
//declare variables separately for reuse without duplicate initialization errors
var allOf, sAdd, styler, i, n;
allOf = document.querySelectorAll('[data-ordinal]'); //find them all
if (allOf==undefined || allOf==null) {
allOf = document.querySelector('[data-ordinal]'); // if there is only one
}
sAdd = "max-width: none;" // create style to add
try{
for(i = 0, n = allOf.length; i < n; i++){ //loop through them all
styler = allOf[i].getAttribute("style");
if (styler==undefined || styler==null) { // if there are no HTML styles set
styler = "";
console.log("No style attributes found for ", i)
}
if (styler!="width: 100%; max-width: 800px;") { // if this isn't a chunk output as expected
continue;
console.log("Attributes not changed for ", i)
}
allOf[i].setAttribute("style",styler+sAdd); // append style
console.log("Attributes set for ", i);
}} catch(error) {
console.error(error);
}
The second:
//declare variables separately for reuse without duplicate initialization errors
var allMore, sAdd2, styler2, j, m
allMore = document.querySelectorAll('.ace_lineWidgetContainer > div'); // first child of class
if (allMore==undefined || allMore==null) {
allMore = document.querySelector('.ace_lineWidgetContainer > div'); // if there is only one
}
sAdd2 = "overflow: visible;" // create style to add
try{
for(j = 0, m = allMore.length; j < m; j++){ //loop through them all
styler2 = allMore[j].getAttribute("style");
if (styler2==undefined || styler2==null) { // if there are no HTML styles set
styler2 = "";
console.log("No styles were found for ", j)
}
allMore[j].setAttribute("style",styler2+sAdd2); // append new styles
allMore[j].style.height = null; // remove the height style
console.log("Attributes set for ", j);
}} catch(error) {
console.error(error);
}
You do not have to use these in a specific order. However, other than that first line (variable initialization), you need to run all of the lines together if you run them at all.
If something goes wrong or RStudio becomes some mutant version of itself, just restart RStudio to reset back to the original sizing.
I'm very new to R and beginner level at programming in general, and trying to figure out how to get hovertext in plotly to display a Japanese string from my dataframe. After venturing through character encoding hell, I've got things mostly worked out but am getting stuck on a single point: Getting the Japanese string to display in the final plot.
plot_ly(df, x = ~cost, y = ~grossSales, type = "scatter", mode = "markers",
hoverinfo = "text",
text = ~paste0("Product name: ", productName,
"<br>Gross: ", grossSales, "<br> Cost: ", cost,
)
)
The problem I encounter is that using 'productName' returns the Japanese string from the dataframe, which causes the plot to fail to render. DOM Inspector's console shows JSON encountering issues with the string (even though it's just encoded in UTF-8).
Using toJSON(productName), I am able to render the table, however this renders the hover textbox with the full information of the productName column (e.g., ["","Product1","Product2","Product3"...]). I only want the name of that specific product; just as 'grossSales' and 'cost' only return one the data specific to that product at each point on the plot.
Is there a way I can execute toJSON() only on each specific instance of 'productName'? (i.e., output should be "Product1" with JSON friendly string format) Alternatively, is there a way I can have plotly read the list output and select only the correct productName?
Stepping away from the problem to continue studying other things, I found a partial solution in using a for-loop:
productNames <- NULL
for (i in 1:nrow(df))
{
productNames <- c(productNames, toJSON(df[i, "productName"]))
}
df$jsonProductNames <- productNames
Using the jsonProductNames variable within plotly, the graph renders and displays only the name for each product! The sole issue remaining is that it is displayed with the JSON [""] formatting around each product's name.
Update:
I've finally got this working fully how I want it. I imagine there are more elegant solutions, and I'd still be interested to learn how to achieve what I originally was looking at if possible (run a function on a variable within R for each time it is encountered in a loop), but here is how I have it working:
colToJSON <- function(df, colStr)
{
JSONCol <- NULL
for (i in 1:nrow(df))
{
JSONCol <- c(JSONCol, toJSON(df[i, colStr]))
}
JSONCol <- gsub("\\[\"", "", JSONCol)
JSONCol <- gsub("\"\\]", "", JSONCol)
return(JSONCol)
}
df$jsonProductNames <- colToJSON(df, "productName")
Been going around for hours with this. My 1st question online on R. Trying to creat a function that contains a loop. The function takes a vector that the user submits like in pollutantmean(4:6) and then it loads a bunch of csv files (in the directory mentioned) and binds them. What is strange (to me) is that if I assign the variable id and then run the loop without using a function, it works! When I put it inside a function so that the user can supply the id vector then it does nothing. Can someone help ? thank you!!!
pollutantmean<-function(id=1:332)
{
#read files
allfiles<-data.frame()
id<-str_pad(id,3,pad = "0")
direct<-"/Users/ped/Documents/LearningR/"
for (i in id) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
}
Your function is missing a return value. (#Roland)
pollutantmean<-function(id=1:332) {
#read files
allfiles<-data.frame()
id<-str_pad(id,3,pad = "0")
direct<-"/Users/ped/Documents/LearningR/"
for (i in id) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
return(allfiles)
}
Edit:
Your mistake was that you did not specify in your function what you want to get out from the function. In R, you create objects inside of function (you could imagine it as different environment) and then specify which object you want it to return.
With my comment about accepting my answer, I meant this: (...To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in...).
Consider even an lapply and do.call which would not need return being last line of function:
pollutantmean <- function(id=1:332) {
id <- str_pad(id,3,pad = "0")
direct_files <- paste0("/Users/ped/Documents/LearningR/", id, ".csv")
# READ FILES INTO LIST AND ROW BIND
allfiles <- do.call(rbind, lapply(direct_files, read.csv))
}
ok, I got it. I was expecting the files that are built to be actually created and show up in the environment of R. But for some reason they don't. But R still does all the calculations. Thanks lot for the replies!!!!
pollutantmean<-function(directory,pollutant,id)
{
#read files
allfiles<-data.frame()
id2<-str_pad(id,3,pad = "0")
direct<-paste("/Users/pedroalbuquerque/Documents/Learning R/",directory,sep="")
for (i in id2) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
#averaging polutants
mean(allfiles[,pollutant],na.rm = TRUE)
}
pollutantmean("specdata","nitrate",23:35)
do you see a way to run a computation in R while waiting for a user input?
I'm writing a script that makes differents types of plots which are defined by user input, but in first lot of data has to be loaded and processed. But in fact, user could start defining what he wants already while the processing is running - that's what I would like to do!
I think package Rdsn might provide the functionality that I need, but I was not able to figure out how.
Thanks!
You didn't give me much context, nor reproducible code, so I will just provide a simple example. I am not familiar with the Rdsn package, so I will use provide a solution I know.
# create a function to prompt the user for some input
readstuff = function(){
stuff = readline(prompt = "Enter some stuff: ")
# Here is where you set the condition for the parameter
# Let's say you want it to be an integer
stuff = as.integer(stuff)
if(is.na(stuff)){
return(readstuff())
} else {
return(stuff)
}
}
parameter = readstuff()
print(parameter)
print(parameter + 10)
The key here is to "source" the script instead of "running" it. You can find the "source" button on the top right of RStudio. You can also use source(yourscript) to source it.
So for every parameter you want to prompt the user for input, just call readstuff(). You can also tweak it a little to make it more general. For example:
# create a function to prompt the user for some input
readstuff = function(promptMessage = "stuff", class = "integer"){
stuff = readline(prompt = paste("Enter the", promptMessage, ": "))
# Here is where you set the condition for the parameter
# Let's say you want it to be an integer
stuff = as(stuff, class)
if(is.na(stuff)){
return(readstuff(promptMessage, class))
} else {
return(stuff)
}
}
plotColor = readstuff("plot color", "character")
size = readstuff("size parameter")
xvarName = readstuff("x axis name", "character")
df = data.frame(x = 1:100, y = 1:100)
library(ggplot2)
p = ggplot(df, aes(x = x, y = y, size = size, color = plotColor)) +
labs(x = xvarName) + geom_point()
print(p)
The if(is.na(stuff)) statements won't work if class is character, but I won't get into details on how to fix that, since this question is mainly about how to wait for user input. There are also ways to suppress the warning messages if the user entered something other than what is intended, but again, a bit off topic to talk about it here.
One important thing you have to watch out for is that anything you want R to print or plot, you need to wrap it with a print() function. Otherwise sourcing it won't print nor plot anything. Also, when typing in a parameter that is intended to be a string or character, don't add quotes. For example, for plotColor, type red instead of "red" in the prompt.
Most of the readline code are referenced from here: