Call UI input via string: R Shiny - r

Thanks in advance.
Wondering if there is a way to call a ui element via string in the server.r file.
Example:
instead of ...
ui.Color <- input$color
do something like ...
ui.Color <- input[,"color"]
...kind of like you could do with a data frame. Issue is class(input) yields reactiveValues so I'm not quite sure how to subset it to get a particular value.
Thanks all!

Got it!
input[["color"]]
Hope this helps others with the same question!

Related

How to call a df variable as the input for a t-test in shiny?

I'm creating a shiny app and I want to include a simple one sample t-test for a continuous numeric variable in a dataframe. For my ui I have:
varSelectInput("Var1", label = "Variable?", data = df),
verbatimTextOutput("ttest")
and the server:
output$ttest <- renderPrint({
t.test(x = input$Var1)
})
I get this error:
object of type 'symbol' is not subsettable
I've seen an example of someone else doing something similar to this, and I can't tell the difference between their code and mine. I apologize because I know this must be a very noob question, but I'm still very new to shiny. Thank you!
Try using
output$ttest <- renderPrint({
t.test(x = df[[input$Var1]])
})
if you haven't I'd start with the documentation for varSelectInput specifically the Server value section specifically. it says "The input value should be used with rlang's rlang::!!(). For example, ggplot2::aes(!!input$variable)." so you may just be missing the !! from the input variable here is more specifics on the !! operator.

R programming (beginner struggles): How can I make this list more efficiƫnt

I just started programming for the first time. I work on a research project with the tuber library. I already set up my api, this works fine.
Now I wanted to get my data into a table.
Therefore I want to combine 'get_stats' and 'get_video_details'.
To use the function 'get_stats' I need a 'video id'. Because I will work with 500 -1000 video Id's, I'm trying to find a way to overcome this:
my_list <- list(
vid_list$items[[1]]$id,
vid_list$items[[2]]$id,
vid_list$items[[3]]$id,
vid_list$items[[4]]$id,
vid_list$items[[5]]$id
......
)
View(my_list)
I figured out I will probably need a to use a loop. But I haven't found a way to work it out. Can anyone of you help me? It would be very much appreciated :)
We extract the 'items', loop through the list with lapply and extract the "id"
lapply(vid_list$items, "[[", "id")

How to print particular JSON value in R?

install.packages("rjson")
library("rjson")
result <- fromJSON(file="provdata.json.txt")
for (i in seq(1,5,1))
{
term <- result$data[i]
print(term)
}
I was wondering what should I put to only print 13.6 and 4.15. Thank you!
I can't check for sure the structure of the json list, as you don't provide a reproducible example (please check https://stackoverflow.com/help/how-to-ask), but I think your problem is that you're not subsetting the list deep enough:
instead of the
term <- result$data[i]
print(term)
bit, try with
print(result$data[[1]][[1]][2])
or something similar. Alternatively, please use dput on a subset of your result variable, so we can help better.

R: avoid repeating $

I'm new here and new to R and I think I have a simple question but don't know how to name it so I can't find any help by searching the web.
I have a data set and want to form a new Data set with several variables from the first one.
The working code looks like this:
em.table2 <- data.frame(em.table$item1,em.table$item2,...[here are some more]...,em.table$item22)
In order to keep it more simple, I want to get rid of the "em.table$"-construction in front of every variable... unfortunately i don't know the function to do so...
I tried it like this, but it didn't work (and is a pretty embarrasing try i guess):
em.table2 <- data.frame(em.table$(item1,item2,item3,item4))
Anyone here to help? Thanks a lot!
Instead of the $ operator, try the following:
em.table2 <- em.table[,c("item1","item2","item3","item4")]
Try with
em.table2 <- with(em.table, data.frame(item1, item2, item3, item4))
But if you just want to subset the data, there are better solutions.

partly upload your input from a file

I have a question about shiny. Can I partly take my input from a file.
For example I have 10 variables, 6 of them I introduce from keyboard and the rest of them, 4 I want to upload from a file. Is something like this possible with shiny?
My server.R looks something like this:
results<-simSIR(nsimulations=input$nsimulations,
ncows=input$ncows,
nvaccinated=input$nvaccinated,
ninfectedinit=input$ninfectedinit,
initvaccination=input$initvaccination,
p=input$p,
freqvacc=input$freqvacc,
noutbreaks=input$noutbreaks,
lambdaiv=input$lambdaiv,
lambdain=input$lambdain,
muiv=input$muiv,
muin=input$muin,
an=input$an,
bn=input$bn,
av=input$av,
bv=input$bv,
cost_vaccination=input$cost_vaccination,
daily_milk=input$daily_milk,
price_liter=input$price_liter,
grafic=F,
parameters=T,
writeitdown=F
)
})
And as you see I take from input all, and I wanted to take some parameters like: lambaiv,lambain,muiv,muin,an,bn,bv,av from a text file.
Thank you in advance!
It's not clear what you have tried so far or what exactly you want to accomplish but this tutorial shows how to work with data files.
The key in that tutorial seems to be:
counties <- readRDS("census-app/data/counties.rds")
The is also an example using the reactiveFileReader at this link.
fileData <- reactiveFileReader(1000, session, 'data.csv', read.csv)

Resources