i just new with rcpp, i have a problem with my rcpp function, when i directly run App, the program display error could not find function "krit". but when i run the function partially with CTRL+R and then run App the program is running well. is there a code for call R function from rcpp function in shiny that i must not run the function partially? in other words, when i directly run App the shiny will running well. this is the example code...
server
library(shiny)
library(Rcpp)
krit <- function(n){
mat <- matrix(1,n,1)
return(mat)
}
cppFunction('
NumericMatrix tes1(int n){
Function krit("krit");
NumericMatrix test = krit(n+1);
return(test);
}
')
shinyServer(function(input, output) {
output$testing <- renderUI({
list(
renderPrint(tes1(3))
)
})
})
ui
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny Text"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
uiOutput("testing")
)
)
))
This is a scoping issue regarding how shiny and Rcpp view the different environments.
What's happening is an issue with accessing the global environment using...
1) Standard Rcpp::Function magic
Rcpp::Function krit("krit");
2) Rcpp::Environment with a global pull yields a missing value.
Rcpp::Environment env = Environment::global_env();
Rcpp::Function krit = env("krit");
Error:
file3d3f43b856e05.cpp:9:45: error: no match for call to '(Rcpp::Environment) (const char [5])'
Thus, the best that can be done to resolve this scoping issue is to pass the R function that you want to use into the compiled C++ function and call it. e.g.
NumericMatrix tes1(int n, Rcpp::Function krit)
Or, you will need to modify the server.R to:
library(shiny)
library(Rcpp)
krit <- function(n){
mat <- matrix(1,n,1)
return(mat)
}
cppFunction('
// Added krit as a function pass
NumericMatrix tes1(int n, Rcpp::Function krit){
NumericMatrix test = krit(n+1);
return(test);
}
')
shinyServer(function(input, output) {
output$testing <- renderUI({
list(
# Added parameter to `tes1` to pass in krit.
renderPrint(tes1(n = 3, krit = krit))
)
})
})
Thus, you should get:
Related
I have a Shiny App with multiple inputs of different kinds (checkboxGroupInput, sliderInput, dateRangeInput ...) with default selected values.
I am trying to display a text message at the top of my dashboardBody if the input values are different from the default ones.
ui <- dashboardPage(
#one of the inputs
dateRangeInput(
"date_reception",
"Sélectionnez une plage de dates",
start = min(dataset_all$date_reception),
end = max(dataset_all$date_reception),
),
#the output to show if input is different from default
textOutput("warning_filters")
----------
)
server <- function(input, output) {
observeEvent(input$date_reception,
{
if ((input$date_reception[1] != min(dataset_all$date_reception)) |
(input$date_reception[2] != max(dataset_all$date_reception))) {
output$warning_filters <-
renderText({
"Warning: filters apply"
})
} else{
NULL
}
})
}
My issue is that the message is correctly displayed when I change one of the dates but does not disappear when I select again the default value (here, min(dataset_all$date_reception) or max(dataset_all$date_reception)).
I have tried playing with ignoreNULL and ignoreInit but nothing changed.
Thank you for your help!
You need to re-assign warning_filters when the condition is false. Now, the warning_filters gets set to the warning text and even though you have the function return NULL when the condition doesn't hold, you don't actually change the values of warning_filters. The code below should work.
observeEvent(input$date_reception,
{
if ((input$date_reception[1] != min(dataset_all$date_reception)) |
(input$date_reception[2] != max(dataset_all$date_reception))) {
output$warning_filters <-
renderText({
"Warning: filters apply"
})
} else{
output$warning_filters <- NULL
}
})
}
I need help with a basic Shiny question. My goal is to make a simple math quiz app (What is 4 x 4?). I want to create values with one button, select a numeric answer, and then press another answer button. My problem is that i cannot find a way to access the values that are stored inside eventReactive. I have simplified the problem in the code below. The goal of this app is to ask for a number, and then to supply it. Thank you in advance!
# Goal: Fetch a number, then input that number, then receive paste("correct")/paste("incorrect)
ui <- fluidPage(
textOutput(outputId = "out"),
numericInput(inputId = "inn",
label = "",
value = 0),
actionButton("answer", "Answer"),
actionButton("question", "New Question"),
)
server <- function(input, output) {
data <- eventReactive(input$question,{
a <- sample.int(10,1)
paste("Enter",a)
})
output$out <- renderText({data()})
}
shinyApp(ui,server)
Here is what I would do
ui <- fluidPage(
textOutput(outputId = "out"),
numericInput(inputId = "inn", label = "", value = 0),
actionButton("answer", "Answer"),
actionButton("question", "New Question"),
)
server <- function(input, output, session) {
data <- reactiveValues(number = NULL)
output$out <- renderText({
if (is.null(data$number))
"Press 'New Question' button"
else
paste("Enter", data$number)
})
observeEvent(input$question, {
data$number = sample(10, 1)
})
observeEvent(input$answer, {
req(data$number, input$inn)
if (data$number == input$inn)
print("Correct")
# Do something exciting
else
print("Incorrect")
# Do something else
})
}
shinyApp(ui,server)
IMO it's good practice to keep reactive data and input/output generation separate. What I mean by that is that in the above example we use
reactiveValues to keep track of the changing data, and
observeEvent to monitor button clicks which may change specific elements of our reactive data,
renderText can print either fixed text or reactive data.
I have a long MathJax expression that I need to print in a Shiny app. Because the expression's so long, it needs to wrap across lines. However, it currently refuses to do so.
I strongly suspect it has to do with the equation not being rendered as conventional text (i.e., it's not being rendered as a string, but as a series of spans), which rules out the usual ways you'd make a div's content wrap.
I know that MathJax supports line wraps (here). I can also get UI equations to wrap using KaTeX (see second code block below), but would prefer to keep everything in MathJax, with how complex the overall app is.
What I've tried:
Via CSS, variants of the following for the "MathJax" class (.MathJax)
word-break: break-all;
word-wrap: break-word;
display: flex;
flex-wrap: wrap;
Setting white-space: nowrap; to white-space: normal; for .MathJax (which shouldn't matter at all, I recognize)
Putting the equation in a wrapper div, and then trying to set that wrapper div's CSS properties as in the two bullets above (plus a variant where I also hardcoded the div's width)
Declaring the following as the first line within the UI function:
tags$script(type="text/x-mathjax-config", "
MathJax.Hub.Config({
\"HTML-CSS\": { linebreaks: { automatic: true;} },
});
"),
Another variant of the above:
tags$script(type="text/x-mathjax-config", "
MathJax.Hub.Config({
\"HTML-CSS\": { linebreaks: { automatic: true;} },
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
"),
The above <script> chunk, plus shifting the MathJax expression in the UI to in-line by replacing the (\\ and )// with $$s.
There are possibly a few different problems all running into each other at once. Thoughts on what those problems may be?
A MWE (no MathJax output wraps)
# UI
ui <- bootstrapPage(
h2("UI"),
# something arbitrary and long
withMathJax(
"\\( f \\left( \\alpha, \\beta_1 | x_i\\right) = \\left(3.5-0.75*-1.414\\right)+ \\left(3.5-0.75*0.189\\right)+ \\left(3.5-0.75*0.026\\right)+ \\left(3.5-0.75*-0.779\\right)+ \\left(3.5-0.75*0.683\\right)+ \\left(3.5-0.75*-0.503\\right)+ \\left(3.5-0.75*-1.18\\right)+ \\left(3.5-0.75*-1.736\\right)+ \\left(3.5-0.75*-1.497\\right)+ \\left(3.5-0.75*-1.12\\right)+ \\left(3.5-0.75*1.263\\right)+ \\left(3.5-0.75*0.512\\right)+ \\left(3.5-0.75*-0.143\\right)+ \\left(3.5-0.75*-0.464\\right)+ \\left(3.5-0.75*-0.016\\right)+ \\left(3.5-0.75*-0.835\\right)+ \\left(3.5-0.75*0.611\\right)+ \\left(3.5-0.75*0\\right)+ \\left(3.5-0.75*-0.117\\right)+ \\left(3.5-0.75*-1.645\\right)+ \\left(3.5-0.75*-1.929\\right)+ \\left(3.5-0.75*-0.026\\right)+ \\left(3.5-0.75*-1.121\\right)+ \\left(3.5-0.75*0.61\\right)+ \\left(3.5-0.75*-0.619\\right)+ \\left(3.5-0.75*0.806\\right)+ \\left(3.5-0.75*-0.123\\right)+ \\left(3.5-0.75*-1.617\\right)+ \\left(3.5-0.75*-0.683\\right)+ \\left(3.5-0.75*0.431\\right)+ \\left(3.5-0.75*2.705\\right)+ \\left(3.5-0.75*0.726\\right)+ \\left(3.5-0.75*-0.483\\right)+ \\left(3.5-0.75*2.135\\right)+ \\left(3.5-0.75*-0.399\\right)+ \\left(3.5-0.75*-0.087\\right)+ \\left(3.5-0.75*-1.004\\right)+ \\left(3.5-0.75*0.18\\right)+ \\left(3.5-0.75*0.229\\right)+ \\left(3.5-0.75*0.671\\right)+ \\left(3.5-0.75*0.234\\right)+ \\left(3.5-0.75*0.874\\right)+ \\left(3.5-0.75*-0.275\\right)+ \\left(3.5-0.75*-0.812\\right)+ \\left(3.5-0.75*0.363\\right)+ \\left(3.5-0.75*-0.549\\right)+ \\left(3.5-0.75*0.32\\right)+ \\left(3.5-0.75*0.667\\right)+ \\left(3.5-0.75*0.44\\right)+ \\left(3.5-0.75*-0.492\\right)+ \\left(3.5-0.75*0.879\\right)+ \\left(3.5-0.75*3.122\\right)+ \\left(3.5-0.75*-0.82\\right)+ \\left(3.5-0.75*-0.65\\right)+ \\left(3.5-0.75*-0.4\\right)+ \\left(3.5-0.75*-0.144\\right)+ \\left(3.5-0.75*1.132\\right)+ \\left(3.5-0.75*0.505\\right)+ \\left(3.5-0.75*0.452\\right)+ \\left(3.5-0.75*0.615\\right)+ \\left(3.5-0.75*-0.94\\right)+ \\left(3.5-0.75*-2.144\\right)+ \\left(3.5-0.75*-1.2\\right)+ \\left(3.5-0.75*-2.893\\right)+ \\left(3.5-0.75*-0.19\\right)+ \\left(3.5-0.75*0.433\\right)+ \\left(3.5-0.75*2.023\\right)+ \\left(3.5-0.75*0.046\\right)+ \\left(3.5-0.75*0.602\\right)+ \\left(3.5-0.75*-0.862\\right)+ \\left(3.5-0.75*-0.687\\right)+ \\left(3.5-0.75*0.907\\right)+ \\left(3.5-0.75*-0.966\\right)+ \\left(3.5-0.75*-1.694\\right)+ \\left(3.5-0.75*1.28\\right)+ \\left(3.5-0.75*1.178\\right)+ \\left(3.5-0.75*-1.077\\right)+ \\left(3.5-0.75*-0.433\\right)+ \\left(3.5-0.75*-0.274\\right)+ \\left(3.5-0.75*-0.725\\right)+ \\left(3.5-0.75*0.735\\right)+ \\left(3.5-0.75*-0.067\\right)+ \\left(3.5-0.75*0.176\\right)+ \\left(3.5-0.75*1.653\\right)+ \\left(3.5-0.75*0.212\\right)+ \\left(3.5-0.75*2.976\\right)+ \\left(3.5-0.75*-0.523\\right)+ \\left(3.5-0.75*-1.426\\right)+ \\left(3.5-0.75*0.673\\right)+ \\left(3.5-0.75*-1.079\\right)+ \\left(3.5-0.75*0.99\\right)+ \\left(3.5-0.75*0.628\\right)+ \\left(3.5-0.75*0.188\\right)+ \\left(3.5-0.75*0.992\\right)+ \\left(3.5-0.75*0.467\\right)+ \\left(3.5-0.75*-0.251\\right)+ \\left(3.5-0.75*1.403\\right)+ \\left(3.5-0.75*0.667\\right)+ \\left(3.5-0.75*1.23\\right)+ \\left(3.5-0.75*1.342\\right)+ \\left(3.5-0.75*1.713\\right)+ \\left(3.5-0.75*0.182\\right)+ \\left(3.5-0.75*0.758\\right)+ \\left(3.5-0.75*0.035\\right)+ \\left(3.5-0.75*0.932\\right)+ \\left(3.5-0.75*0.702\\right)+ \\left(3.5-0.75*0.436\\right)+ \\left(3.5-0.75*-1.355\\right)+ \\left(3.5-0.75*-1.318\\right)+ \\left(3.5-0.75*-0.109\\right)+ \\left(3.5-0.75*0.437\\right)+ \\left(3.5-0.75*-0.842\\right)+ \\left(3.5-0.75*0.977\\right)+ \\left(3.5-0.75*-1.503\\right)+ \\left(3.5-0.75*-0.253\\right)+ \\left(3.5-0.75*-1.166\\right)+ \\left(3.5-0.75*-0.274\\right)+ \\left(3.5-0.75*-1.083\\right)+ \\left(3.5-0.75*0.086\\right)+ \\left(3.5-0.75*0.807\\right)+ \\left(3.5-0.75*-0.068\\right)+ \\left(3.5-0.75*0.682\\right)+ \\left(3.5-0.75*0.26\\right)+ \\left(3.5-0.75*1.225\\right)+ \\left(3.5-0.75*1.82\\right)+ \\left(3.5-0.75*-1.444\\right)+ \\left(3.5-0.75*-0.579\\right)+ \\left(3.5-0.75*-0.945\\right)+ \\left(3.5-0.75*-0.743\\right)+ \\left(3.5-0.75*-0.423\\right)+ \\left(3.5-0.75*-0.086\\right)+ \\left(3.5-0.75*-0.105\\right)+ \\left(3.5-0.75*0.385\\right)+ \\left(3.5-0.75*0.733\\right)+ \\left(3.5-0.75*-1.131\\right)+ \\left(3.5-0.75*3.334\\right)+ \\left(3.5-0.75*-0.254\\right)+ \\left(3.5-0.75*0.304\\right)+ \\left(3.5-0.75*1.234\\right)+ \\left(3.5-0.75*1.144\\right)+ \\left(3.5-0.75*0.558\\right)+ \\left(3.5-0.75*-0.675\\right)+ \\left(3.5-0.75*-1.729\\right)+ \\left(3.5-0.75*-1.504\\right)+ \\left(3.5-0.75*-1.951\\right)+ \\left(3.5-0.75*0.203\\right)+ \\left(3.5-0.75*-0.154\\right)+ \\left(3.5-0.75*-0.545\\right)+ \\left(3.5-0.75*-0.913\\right)+ \\left(3.5-0.75*-1.11\\right) \\)"
),
h2("Server"),
uiOutput("eqSrv")
)
# Server
server <- function(input, output) {
output$eqSrv<- renderUI({
# something arbitrary and long
withMathJax(
"\\( f \\left( \\alpha, \\beta_1 | x_i\\right) = \\left(3.5-0.75*-1.414\\right)+ \\left(3.5-0.75*0.189\\right)+ \\left(3.5-0.75*0.026\\right)+ \\left(3.5-0.75*-0.779\\right)+ \\left(3.5-0.75*0.683\\right)+ \\left(3.5-0.75*-0.503\\right)+ \\left(3.5-0.75*-1.18\\right)+ \\left(3.5-0.75*-1.736\\right)+ \\left(3.5-0.75*-1.497\\right)+ \\left(3.5-0.75*-1.12\\right)+ \\left(3.5-0.75*1.263\\right)+ \\left(3.5-0.75*0.512\\right)+ \\left(3.5-0.75*-0.143\\right)+ \\left(3.5-0.75*-0.464\\right)+ \\left(3.5-0.75*-0.016\\right)+ \\left(3.5-0.75*-0.835\\right)+ \\left(3.5-0.75*0.611\\right)+ \\left(3.5-0.75*0\\right)+ \\left(3.5-0.75*-0.117\\right)+ \\left(3.5-0.75*-1.645\\right)+ \\left(3.5-0.75*-1.929\\right)+ \\left(3.5-0.75*-0.026\\right)+ \\left(3.5-0.75*-1.121\\right)+ \\left(3.5-0.75*0.61\\right)+ \\left(3.5-0.75*-0.619\\right)+ \\left(3.5-0.75*0.806\\right)+ \\left(3.5-0.75*-0.123\\right)+ \\left(3.5-0.75*-1.617\\right)+ \\left(3.5-0.75*-0.683\\right)+ \\left(3.5-0.75*0.431\\right)+ \\left(3.5-0.75*2.705\\right)+ \\left(3.5-0.75*0.726\\right)+ \\left(3.5-0.75*-0.483\\right)+ \\left(3.5-0.75*2.135\\right)+ \\left(3.5-0.75*-0.399\\right)+ \\left(3.5-0.75*-0.087\\right)+ \\left(3.5-0.75*-1.004\\right)+ \\left(3.5-0.75*0.18\\right)+ \\left(3.5-0.75*0.229\\right)+ \\left(3.5-0.75*0.671\\right)+ \\left(3.5-0.75*0.234\\right)+ \\left(3.5-0.75*0.874\\right)+ \\left(3.5-0.75*-0.275\\right)+ \\left(3.5-0.75*-0.812\\right)+ \\left(3.5-0.75*0.363\\right)+ \\left(3.5-0.75*-0.549\\right)+ \\left(3.5-0.75*0.32\\right)+ \\left(3.5-0.75*0.667\\right)+ \\left(3.5-0.75*0.44\\right)+ \\left(3.5-0.75*-0.492\\right)+ \\left(3.5-0.75*0.879\\right)+ \\left(3.5-0.75*3.122\\right)+ \\left(3.5-0.75*-0.82\\right)+ \\left(3.5-0.75*-0.65\\right)+ \\left(3.5-0.75*-0.4\\right)+ \\left(3.5-0.75*-0.144\\right)+ \\left(3.5-0.75*1.132\\right)+ \\left(3.5-0.75*0.505\\right)+ \\left(3.5-0.75*0.452\\right)+ \\left(3.5-0.75*0.615\\right)+ \\left(3.5-0.75*-0.94\\right)+ \\left(3.5-0.75*-2.144\\right)+ \\left(3.5-0.75*-1.2\\right)+ \\left(3.5-0.75*-2.893\\right)+ \\left(3.5-0.75*-0.19\\right)+ \\left(3.5-0.75*0.433\\right)+ \\left(3.5-0.75*2.023\\right)+ \\left(3.5-0.75*0.046\\right)+ \\left(3.5-0.75*0.602\\right)+ \\left(3.5-0.75*-0.862\\right)+ \\left(3.5-0.75*-0.687\\right)+ \\left(3.5-0.75*0.907\\right)+ \\left(3.5-0.75*-0.966\\right)+ \\left(3.5-0.75*-1.694\\right)+ \\left(3.5-0.75*1.28\\right)+ \\left(3.5-0.75*1.178\\right)+ \\left(3.5-0.75*-1.077\\right)+ \\left(3.5-0.75*-0.433\\right)+ \\left(3.5-0.75*-0.274\\right)+ \\left(3.5-0.75*-0.725\\right)+ \\left(3.5-0.75*0.735\\right)+ \\left(3.5-0.75*-0.067\\right)+ \\left(3.5-0.75*0.176\\right)+ \\left(3.5-0.75*1.653\\right)+ \\left(3.5-0.75*0.212\\right)+ \\left(3.5-0.75*2.976\\right)+ \\left(3.5-0.75*-0.523\\right)+ \\left(3.5-0.75*-1.426\\right)+ \\left(3.5-0.75*0.673\\right)+ \\left(3.5-0.75*-1.079\\right)+ \\left(3.5-0.75*0.99\\right)+ \\left(3.5-0.75*0.628\\right)+ \\left(3.5-0.75*0.188\\right)+ \\left(3.5-0.75*0.992\\right)+ \\left(3.5-0.75*0.467\\right)+ \\left(3.5-0.75*-0.251\\right)+ \\left(3.5-0.75*1.403\\right)+ \\left(3.5-0.75*0.667\\right)+ \\left(3.5-0.75*1.23\\right)+ \\left(3.5-0.75*1.342\\right)+ \\left(3.5-0.75*1.713\\right)+ \\left(3.5-0.75*0.182\\right)+ \\left(3.5-0.75*0.758\\right)+ \\left(3.5-0.75*0.035\\right)+ \\left(3.5-0.75*0.932\\right)+ \\left(3.5-0.75*0.702\\right)+ \\left(3.5-0.75*0.436\\right)+ \\left(3.5-0.75*-1.355\\right)+ \\left(3.5-0.75*-1.318\\right)+ \\left(3.5-0.75*-0.109\\right)+ \\left(3.5-0.75*0.437\\right)+ \\left(3.5-0.75*-0.842\\right)+ \\left(3.5-0.75*0.977\\right)+ \\left(3.5-0.75*-1.503\\right)+ \\left(3.5-0.75*-0.253\\right)+ \\left(3.5-0.75*-1.166\\right)+ \\left(3.5-0.75*-0.274\\right)+ \\left(3.5-0.75*-1.083\\right)+ \\left(3.5-0.75*0.086\\right)+ \\left(3.5-0.75*0.807\\right)+ \\left(3.5-0.75*-0.068\\right)+ \\left(3.5-0.75*0.682\\right)+ \\left(3.5-0.75*0.26\\right)+ \\left(3.5-0.75*1.225\\right)+ \\left(3.5-0.75*1.82\\right)+ \\left(3.5-0.75*-1.444\\right)+ \\left(3.5-0.75*-0.579\\right)+ \\left(3.5-0.75*-0.945\\right)+ \\left(3.5-0.75*-0.743\\right)+ \\left(3.5-0.75*-0.423\\right)+ \\left(3.5-0.75*-0.086\\right)+ \\left(3.5-0.75*-0.105\\right)+ \\left(3.5-0.75*0.385\\right)+ \\left(3.5-0.75*0.733\\right)+ \\left(3.5-0.75*-1.131\\right)+ \\left(3.5-0.75*3.334\\right)+ \\left(3.5-0.75*-0.254\\right)+ \\left(3.5-0.75*0.304\\right)+ \\left(3.5-0.75*1.234\\right)+ \\left(3.5-0.75*1.144\\right)+ \\left(3.5-0.75*0.558\\right)+ \\left(3.5-0.75*-0.675\\right)+ \\left(3.5-0.75*-1.729\\right)+ \\left(3.5-0.75*-1.504\\right)+ \\left(3.5-0.75*-1.951\\right)+ \\left(3.5-0.75*0.203\\right)+ \\left(3.5-0.75*-0.154\\right)+ \\left(3.5-0.75*-0.545\\right)+ \\left(3.5-0.75*-0.913\\right)+ \\left(3.5-0.75*-1.11\\right) \\)"
)
})
}
# Run
shinyApp(ui = ui, server = server)
A MWE with KaTeX (which does wrap for the UI-printed eq, but not server-printed eq. Would also prefer to do everything with MathJax.)
# Define the UI
ui <- bootstrapPage(
tags$head(
tags$link(rel="stylesheet",
href="https://cdn.jsdelivr.net/npm/katex#0.10.1/dist/katex.min.css",
integrity="sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ",
crossorigin="anonymous"),
HTML('<script defer src="https://cdn.jsdelivr.net/npm/katex#0.10.1/dist/katex.min.js" integrity="sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij" crossorigin="anonymous"></script>'),
HTML('<script defer src="https://cdn.jsdelivr.net/npm/katex#0.10.1/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"></script>'),
HTML('
<script>
document.addEventListener("DOMContentLoaded", function(){
renderMathInElement(document.body, {
delimiters: [{left: "$", right: "$", display: false}]
});
})
</script>')
),
h2("UI"),
withMathJax(),
# something arbitrary and long
p(" $ f\\left( \\alpha, \\beta_1 | x_i\\right) = \\left(3.5-0.75*-1.414\\right)+ \\left(3.5-0.75*0.189\\right)+ \\left(3.5-0.75*0.026\\right)+ \\left(3.5-0.75*-0.779\\right)+ \\left(3.5-0.75*0.683\\right)+ \\left(3.5-0.75*-0.503\\right)+ \\left(3.5-0.75*-1.18\\right)+ \\left(3.5-0.75*-1.736\\right)+ \\left(3.5-0.75*-1.497\\right)+ \\left(3.5-0.75*-1.12\\right)+ \\left(3.5-0.75*1.263\\right)+ \\left(3.5-0.75*0.512\\right)+ \\left(3.5-0.75*-0.143\\right)+ \\left(3.5-0.75*-0.464\\right)+ \\left(3.5-0.75*-0.016\\right)+ \\left(3.5-0.75*-0.835\\right)+ \\left(3.5-0.75*0.611\\right)+ \\left(3.5-0.75*0\\right)+ \\left(3.5-0.75*-0.117\\right)+ \\left(3.5-0.75*-1.645\\right)+ \\left(3.5-0.75*-1.929\\right)+ \\left(3.5-0.75*-0.026\\right)+ \\left(3.5-0.75*-1.121\\right)+ \\left(3.5-0.75*0.61\\right)+ \\left(3.5-0.75*-0.619\\right)+ \\left(3.5-0.75*0.806\\right)+ \\left(3.5-0.75*-0.123\\right)+ \\left(3.5-0.75*-1.617\\right)+ \\left(3.5-0.75*-0.683\\right)+ \\left(3.5-0.75*0.431\\right)+ \\left(3.5-0.75*2.705\\right)+ \\left(3.5-0.75*0.726\\right)+ \\left(3.5-0.75*-0.483\\right)+ \\left(3.5-0.75*2.135\\right)+ \\left(3.5-0.75*-0.399\\right)+ \\left(3.5-0.75*-0.087\\right)+ \\left(3.5-0.75*-1.004\\right)+ \\left(3.5-0.75*0.18\\right)+ \\left(3.5-0.75*0.229\\right)+ \\left(3.5-0.75*0.671\\right)+ \\left(3.5-0.75*0.234\\right)+ \\left(3.5-0.75*0.874\\right)+ \\left(3.5-0.75*-0.275\\right)+ \\left(3.5-0.75*-0.812\\right)+ \\left(3.5-0.75*0.363\\right)+ \\left(3.5-0.75*-0.549\\right)+ \\left(3.5-0.75*0.32\\right)+ \\left(3.5-0.75*0.667\\right)+ \\left(3.5-0.75*0.44\\right)+ \\left(3.5-0.75*-0.492\\right)+ \\left(3.5-0.75*0.879\\right)+ \\left(3.5-0.75*3.122\\right)+ \\left(3.5-0.75*-0.82\\right)+ \\left(3.5-0.75*-0.65\\right)+ \\left(3.5-0.75*-0.4\\right)+ \\left(3.5-0.75*-0.144\\right)+ \\left(3.5-0.75*1.132\\right)+ \\left(3.5-0.75*0.505\\right)+ \\left(3.5-0.75*0.452\\right)+ \\left(3.5-0.75*0.615\\right)+ \\left(3.5-0.75*-0.94\\right)+ \\left(3.5-0.75*-2.144\\right)+ \\left(3.5-0.75*-1.2\\right)+ \\left(3.5-0.75*-2.893\\right)+ \\left(3.5-0.75*-0.19\\right)+ \\left(3.5-0.75*0.433\\right)+ \\left(3.5-0.75*2.023\\right)+ \\left(3.5-0.75*0.046\\right)+ \\left(3.5-0.75*0.602\\right)+ \\left(3.5-0.75*-0.862\\right)+ \\left(3.5-0.75*-0.687\\right)+ \\left(3.5-0.75*0.907\\right)+ \\left(3.5-0.75*-0.966\\right)+ \\left(3.5-0.75*-1.694\\right)+ \\left(3.5-0.75*1.28\\right)+ \\left(3.5-0.75*1.178\\right)+ \\left(3.5-0.75*-1.077\\right)+ \\left(3.5-0.75*-0.433\\right)+ \\left(3.5-0.75*-0.274\\right)+ \\left(3.5-0.75*-0.725\\right)+ \\left(3.5-0.75*0.735\\right)+ \\left(3.5-0.75*-0.067\\right)+ \\left(3.5-0.75*0.176\\right)+ \\left(3.5-0.75*1.653\\right)+ \\left(3.5-0.75*0.212\\right)+ \\left(3.5-0.75*2.976\\right)+ \\left(3.5-0.75*-0.523\\right)+ \\left(3.5-0.75*-1.426\\right)+ \\left(3.5-0.75*0.673\\right)+ \\left(3.5-0.75*-1.079\\right)+ \\left(3.5-0.75*0.99\\right)+ \\left(3.5-0.75*0.628\\right)+ \\left(3.5-0.75*0.188\\right)+ \\left(3.5-0.75*0.992\\right)+ \\left(3.5-0.75*0.467\\right)+ \\left(3.5-0.75*-0.251\\right)+ \\left(3.5-0.75*1.403\\right)+ \\left(3.5-0.75*0.667\\right)+ \\left(3.5-0.75*1.23\\right)+ \\left(3.5-0.75*1.342\\right)+ \\left(3.5-0.75*1.713\\right)+ \\left(3.5-0.75*0.182\\right)+ \\left(3.5-0.75*0.758\\right)+ \\left(3.5-0.75*0.035\\right)+ \\left(3.5-0.75*0.932\\right)+ \\left(3.5-0.75*0.702\\right)+ \\left(3.5-0.75*0.436\\right)+ \\left(3.5-0.75*-1.355\\right)+ \\left(3.5-0.75*-1.318\\right)+ \\left(3.5-0.75*-0.109\\right)+ \\left(3.5-0.75*0.437\\right)+ \\left(3.5-0.75*-0.842\\right)+ \\left(3.5-0.75*0.977\\right)+ \\left(3.5-0.75*-1.503\\right)+ \\left(3.5-0.75*-0.253\\right)+ \\left(3.5-0.75*-1.166\\right)+ \\left(3.5-0.75*-0.274\\right)+ \\left(3.5-0.75*-1.083\\right)+ \\left(3.5-0.75*0.086\\right)+ \\left(3.5-0.75*0.807\\right)+ \\left(3.5-0.75*-0.068\\right)+ \\left(3.5-0.75*0.682\\right)+ \\left(3.5-0.75*0.26\\right)+ \\left(3.5-0.75*1.225\\right)+ \\left(3.5-0.75*1.82\\right)+ \\left(3.5-0.75*-1.444\\right)+ \\left(3.5-0.75*-0.579\\right)+ \\left(3.5-0.75*-0.945\\right)+ \\left(3.5-0.75*-0.743\\right)+ \\left(3.5-0.75*-0.423\\right)+ \\left(3.5-0.75*-0.086\\right)+ \\left(3.5-0.75*-0.105\\right)+ \\left(3.5-0.75*0.385\\right)+ \\left(3.5-0.75*0.733\\right)+ \\left(3.5-0.75*-1.131\\right)+ \\left(3.5-0.75*3.334\\right)+ \\left(3.5-0.75*-0.254\\right)+ \\left(3.5-0.75*0.304\\right)+ \\left(3.5-0.75*1.234\\right)+ \\left(3.5-0.75*1.144\\right)+ \\left(3.5-0.75*0.558\\right)+ \\left(3.5-0.75*-0.675\\right)+ \\left(3.5-0.75*-1.729\\right)+ \\left(3.5-0.75*-1.504\\right)+ \\left(3.5-0.75*-1.951\\right)+ \\left(3.5-0.75*0.203\\right)+ \\left(3.5-0.75*-0.154\\right)+ \\left(3.5-0.75*-0.545\\right)+ \\left(3.5-0.75*-0.913\\right)+ \\left(3.5-0.75*-1.11\\right)$ "
),
h2("Server"),
uiOutput("eqSrv")
)
# Define the server code
server <- function(input, output) {
output$eqSrv<- renderUI({
# something arbitrary and long
withMathJax(
"$$ f\\left( \\alpha, \\beta_1 | x_i\\right) = \\left(3.5-0.75*-1.414\\right)+ \\left(3.5-0.75*0.189\\right)+ \\left(3.5-0.75*0.026\\right)+ \\left(3.5-0.75*-0.779\\right)+ \\left(3.5-0.75*0.683\\right)+ \\left(3.5-0.75*-0.503\\right)+ \\left(3.5-0.75*-1.18\\right)+ \\left(3.5-0.75*-1.736\\right)+ \\left(3.5-0.75*-1.497\\right)+ \\left(3.5-0.75*-1.12\\right)+ \\left(3.5-0.75*1.263\\right)+ \\left(3.5-0.75*0.512\\right)+ \\left(3.5-0.75*-0.143\\right)+ \\left(3.5-0.75*-0.464\\right)+ \\left(3.5-0.75*-0.016\\right)+ \\left(3.5-0.75*-0.835\\right)+ \\left(3.5-0.75*0.611\\right)+ \\left(3.5-0.75*0\\right)+ \\left(3.5-0.75*-0.117\\right)+ \\left(3.5-0.75*-1.645\\right)+ \\left(3.5-0.75*-1.929\\right)+ \\left(3.5-0.75*-0.026\\right)+ \\left(3.5-0.75*-1.121\\right)+ \\left(3.5-0.75*0.61\\right)+ \\left(3.5-0.75*-0.619\\right)+ \\left(3.5-0.75*0.806\\right)+ \\left(3.5-0.75*-0.123\\right)+ \\left(3.5-0.75*-1.617\\right)+ \\left(3.5-0.75*-0.683\\right)+ \\left(3.5-0.75*0.431\\right)+ \\left(3.5-0.75*2.705\\right)+ \\left(3.5-0.75*0.726\\right)+ \\left(3.5-0.75*-0.483\\right)+ \\left(3.5-0.75*2.135\\right)+ \\left(3.5-0.75*-0.399\\right)+ \\left(3.5-0.75*-0.087\\right)+ \\left(3.5-0.75*-1.004\\right)+ \\left(3.5-0.75*0.18\\right)+ \\left(3.5-0.75*0.229\\right)+ \\left(3.5-0.75*0.671\\right)+ \\left(3.5-0.75*0.234\\right)+ \\left(3.5-0.75*0.874\\right)+ \\left(3.5-0.75*-0.275\\right)+ \\left(3.5-0.75*-0.812\\right)+ \\left(3.5-0.75*0.363\\right)+ \\left(3.5-0.75*-0.549\\right)+ \\left(3.5-0.75*0.32\\right)+ \\left(3.5-0.75*0.667\\right)+ \\left(3.5-0.75*0.44\\right)+ \\left(3.5-0.75*-0.492\\right)+ \\left(3.5-0.75*0.879\\right)+ \\left(3.5-0.75*3.122\\right)+ \\left(3.5-0.75*-0.82\\right)+ \\left(3.5-0.75*-0.65\\right)+ \\left(3.5-0.75*-0.4\\right)+ \\left(3.5-0.75*-0.144\\right)+ \\left(3.5-0.75*1.132\\right)+ \\left(3.5-0.75*0.505\\right)+ \\left(3.5-0.75*0.452\\right)+ \\left(3.5-0.75*0.615\\right)+ \\left(3.5-0.75*-0.94\\right)+ \\left(3.5-0.75*-2.144\\right)+ \\left(3.5-0.75*-1.2\\right)+ \\left(3.5-0.75*-2.893\\right)+ \\left(3.5-0.75*-0.19\\right)+ \\left(3.5-0.75*0.433\\right)+ \\left(3.5-0.75*2.023\\right)+ \\left(3.5-0.75*0.046\\right)+ \\left(3.5-0.75*0.602\\right)+ \\left(3.5-0.75*-0.862\\right)+ \\left(3.5-0.75*-0.687\\right)+ \\left(3.5-0.75*0.907\\right)+ \\left(3.5-0.75*-0.966\\right)+ \\left(3.5-0.75*-1.694\\right)+ \\left(3.5-0.75*1.28\\right)+ \\left(3.5-0.75*1.178\\right)+ \\left(3.5-0.75*-1.077\\right)+ \\left(3.5-0.75*-0.433\\right)+ \\left(3.5-0.75*-0.274\\right)+ \\left(3.5-0.75*-0.725\\right)+ \\left(3.5-0.75*0.735\\right)+ \\left(3.5-0.75*-0.067\\right)+ \\left(3.5-0.75*0.176\\right)+ \\left(3.5-0.75*1.653\\right)+ \\left(3.5-0.75*0.212\\right)+ \\left(3.5-0.75*2.976\\right)+ \\left(3.5-0.75*-0.523\\right)+ \\left(3.5-0.75*-1.426\\right)+ \\left(3.5-0.75*0.673\\right)+ \\left(3.5-0.75*-1.079\\right)+ \\left(3.5-0.75*0.99\\right)+ \\left(3.5-0.75*0.628\\right)+ \\left(3.5-0.75*0.188\\right)+ \\left(3.5-0.75*0.992\\right)+ \\left(3.5-0.75*0.467\\right)+ \\left(3.5-0.75*-0.251\\right)+ \\left(3.5-0.75*1.403\\right)+ \\left(3.5-0.75*0.667\\right)+ \\left(3.5-0.75*1.23\\right)+ \\left(3.5-0.75*1.342\\right)+ \\left(3.5-0.75*1.713\\right)+ \\left(3.5-0.75*0.182\\right)+ \\left(3.5-0.75*0.758\\right)+ \\left(3.5-0.75*0.035\\right)+ \\left(3.5-0.75*0.932\\right)+ \\left(3.5-0.75*0.702\\right)+ \\left(3.5-0.75*0.436\\right)+ \\left(3.5-0.75*-1.355\\right)+ \\left(3.5-0.75*-1.318\\right)+ \\left(3.5-0.75*-0.109\\right)+ \\left(3.5-0.75*0.437\\right)+ \\left(3.5-0.75*-0.842\\right)+ \\left(3.5-0.75*0.977\\right)+ \\left(3.5-0.75*-1.503\\right)+ \\left(3.5-0.75*-0.253\\right)+ \\left(3.5-0.75*-1.166\\right)+ \\left(3.5-0.75*-0.274\\right)+ \\left(3.5-0.75*-1.083\\right)+ \\left(3.5-0.75*0.086\\right)+ \\left(3.5-0.75*0.807\\right)+ \\left(3.5-0.75*-0.068\\right)+ \\left(3.5-0.75*0.682\\right)+ \\left(3.5-0.75*0.26\\right)+ \\left(3.5-0.75*1.225\\right)+ \\left(3.5-0.75*1.82\\right)+ \\left(3.5-0.75*-1.444\\right)+ \\left(3.5-0.75*-0.579\\right)+ \\left(3.5-0.75*-0.945\\right)+ \\left(3.5-0.75*-0.743\\right)+ \\left(3.5-0.75*-0.423\\right)+ \\left(3.5-0.75*-0.086\\right)+ \\left(3.5-0.75*-0.105\\right)+ \\left(3.5-0.75*0.385\\right)+ \\left(3.5-0.75*0.733\\right)+ \\left(3.5-0.75*-1.131\\right)+ \\left(3.5-0.75*3.334\\right)+ \\left(3.5-0.75*-0.254\\right)+ \\left(3.5-0.75*0.304\\right)+ \\left(3.5-0.75*1.234\\right)+ \\left(3.5-0.75*1.144\\right)+ \\left(3.5-0.75*0.558\\right)+ \\left(3.5-0.75*-0.675\\right)+ \\left(3.5-0.75*-1.729\\right)+ \\left(3.5-0.75*-1.504\\right)+ \\left(3.5-0.75*-1.951\\right)+ \\left(3.5-0.75*0.203\\right)+ \\left(3.5-0.75*-0.154\\right)+ \\left(3.5-0.75*-0.545\\right)+ \\left(3.5-0.75*-0.913\\right)+ \\left(3.5-0.75*-1.11\\right) $$"
)
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
The MathJax config works if you put it in tags$head:
tags$head(tags$script(type = "text/x-mathjax-config",
'MathJax.Hub.Config({
"HTML-CSS": { linebreaks: { automatic: true } },
SVG: { linebreaks: { automatic: true } }
});')),
I'm able to load a saved file as an image but unable to use gganimate to do it directly. Alternate ways of rendering GIFs would be nice to know but knowing how to render gganimate specifically would really solve my problem.
library(gapminder)
library(ggplot2)
library(shiny)
library(gganimate)
theme_set(theme_bw())
ui <- basicPage(
plotOutput("plot1")
)
server <- function(input, output) {
output$plot1 <- renderPlot({
p = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
geom_point() +
scale_x_log10()
gg_animate(p)
})
}
shinyApp(ui, server)
Now that there's a newer breaking version of gganimate, #kt.leap's answer is deprecated. Here's what worked for me with the new gganimate:
library(gapminder)
library(ggplot2)
library(shiny)
library(gganimate)
theme_set(theme_bw())
ui <- basicPage(
imageOutput("plot1"))
server <- function(input, output) {
output$plot1 <- renderImage({
# A temp file to save the output.
# This file will be removed later by renderImage
outfile <- tempfile(fileext='.gif')
# now make the animation
p = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop,
color = continent)) + geom_point() + scale_x_log10() +
transition_time(year) # New
anim_save("outfile.gif", animate(p)) # New
# Return a list containing the filename
list(src = "outfile.gif",
contentType = 'image/gif'
# width = 400,
# height = 300,
# alt = "This is alternate text"
)}, deleteFile = TRUE)}
shinyApp(ui, server)
I was dealing with the same issue and found only your question and no answers... But the way you phrased it reminded me that renderPlot is finicky:
it won’t send just any image file to the browser – the image must be generated by code that uses R’s graphical output device system. Other methods of creating images can’t be sent by renderPlot()... The solution in these cases is the renderImage() function. source
Modifying the code from that article gives you the following:
library(gapminder)
library(ggplot2)
library(shiny)
library(gganimate)
theme_set(theme_bw())
ui <- basicPage(
imageOutput("plot1"))
server <- function(input, output) {
output$plot1 <- renderImage({
# A temp file to save the output.
# This file will be removed later by renderImage
outfile <- tempfile(fileext='.gif')
# now make the animation
p = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop,
color = continent, frame = year)) + geom_point() + scale_x_log10()
gg_animate(p,"outfile.gif")
# Return a list containing the filename
list(src = "outfile.gif",
contentType = 'image/gif'
# width = 400,
# height = 300,
# alt = "This is alternate text"
)}, deleteFile = TRUE)}
shinyApp(ui, server)
I'm trying to reproduce an R example based in R help.
This is an example for cxxfunction from inline package.
require(Rcpp)
require(inline)
# Rcpp plugin
if( require( Rcpp ) ){
fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
return wrap( as<int>(x) * as<double>(y) ) ;
', plugin = "Rcpp" )
fx( 2L, 5 )
## equivalent shorter form using rcpp()
fx <- rcpp(signature(x = "integer", y = "numeric"),
' return wrap( as<int>(x) * as<double>(y) ) ; ')
}
I got this message:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Warning message:
running command 'make -f "C:/PROGRA~1/R/R-30~1.3/etc/i386/Makeconf" -f "C:/PROGRA~1/R/R-30~1.3/share/make/winshlib.mk" ....
And in addition:
Warning message:
running command 'C:/PROGRA~1/R/R-30~1.3/bin/i386/R CMD SHLIB file3a86e316ef8.cpp ...
I'm using:
platform i386-w64-mingw32 and R-3.0.3
As Kevin said, its easier to use attributes. The attribute // [[Rcpp::export]] does all the work for you.
With the current versions on R and Rcpp installed create a file called test.cpp to put this code:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double fx(int x, double y){
return x*y;
}
/*** R
fx(2L,5)
fx(2L,5.1)
*/
Then in a R session run: Rcpp::sourceCpp('test.cpp')
That should work, if you have followed the instructions for installing R, R-tools and set PATH variables (if on windows) correctly.